<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>文本框</title>
    <base href="../../../../../"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" >
    <meta name="generator" content="www.leipi.org" />
    <link rel="stylesheet" href="plug-in/Formdesign/js/ueditor/formdesign/bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="plug-in/Formdesign/js/ueditor/formdesign/leipi.style.css">
    <script type="text/javascript" src="plug-in/Formdesign/js/ueditor/formdesign/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="plug-in/Formdesign/js/ueditor/dialogs/internal.js"></script>
    <script type="text/javascript">
/* Thank you by  
http://www.alt-tag.com/blog/2006/02/ie-dom-bugs/ */
function createElement(type, name)
{     
    var element = null;     
    try {        
        element = document.createElement('<'+type+' name="'+name+'">');     
    } catch (e) {}   
    if(element==null) {     
        element = document.createElement(type);     
        element.name = name;     
    } 
    return element;     
}
    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
      
     <tr>
        <th><span>控件名称</span><span class="label label-important">*</span></th>
        <th><span>默认值</span> </th>
    </tr>
    <tr>
        <td><input type="text" id="orgname" placeholder="必填项"></td>
        <td><input type="text" id="orgvalue" placeholder="无则不填"></td>
    </tr>
    <tr>
        <th><span>数据源</span></th>
        <th><span>字段</span> </th>
    </tr>
    <tr>
        <td>
           <select id="datasource"></select>
        </td>
        <td>
           <select id="field">
           	  <option selected value=''>请先选择数据源</option>
           </select>
        </td>
    </tr>
    <tr>
        <th><span>数据类型</span> </th>
        <th><span>对齐方式</span> </th>
    </tr>
    <tr>
        <td>
             <select id="orgtype">
                <option value="text">普通文本</option>
                <option value="email">邮箱地址</option>
                <option value="int">整数</option>
                <option value="float">小数</option>
                <option value="idcard">身份证号码</option>
                <option value="standardDate">日期(yyyy-MM-dd) </option>
                <option value="fullDate">日期(yyyy-MM-dd HH:mm:ss)</option>
            </select>   
        </td>
        <td>
            <select id="orgalign">
                <option value="left" >左对齐</option>
                <option value="center">居中对齐</option>
                <option value="right">右对齐</option>
            </select>
        </td>
    </tr>
    <tr>
        <th><span>&nbsp;&nbsp;&nbsp;&nbsp;长&nbsp;&nbsp;X&nbsp;&nbsp;宽&nbsp;&nbsp;&nbsp;&&nbsp;&nbsp;&nbsp;字体大小</span> </th>
        <th><span>可见性</span> </th>
    </tr>
    <tr>
        <td>
            <input id="orgwidth" type="text" value="150" class="input-small span1" placeholder="auto"/>
            X
            <input id="orgheight" type="text" value="" class="input-small span1" placeholder="auto"/>
            &
            <input id="orgfontsize" type="text"  value="" class="input-small span1" placeholder="auto"/> px

        </td>
        <td>
            <label class="checkbox inline"><input id="orghide" type="checkbox"/> 隐藏 </label>
        </td>
    </tr>
	<tr>
        <th><span>检验规则</span> </th>
        <th></th>
    </tr>
    <tr>
        <td>
     		 <select type="text" id="datatype" name="datatype" value="" >
             </select>
        </td>
        <td>
            <label class="checkbox inline"> </label>
        </td>
    </tr>
    </table>
</div>
<script type="text/javascript">
var oNode = null,thePlugins = 'text';
window.onload = function() {
    if( UE.plugins[thePlugins].editdom ){
        oNode = UE.plugins[thePlugins].editdom;
		var gValue = '';
		if(oNode.getAttribute('value'))
			gValue = oNode.getAttribute('value').replace(/&quot;/g,"\"");
		var gTitle=oNode.getAttribute('title').replace(/&quot;/g,"\""),gHidden=oNode.getAttribute('orghide'),gFontSize=oNode.getAttribute('orgfontsize'),gAlign=oNode.getAttribute('orgalign'),gWidth=oNode.getAttribute('orgwidth'),gHeight=oNode.getAttribute('orgheight'),gType=oNode.getAttribute('orgtype');
		gValue = gValue==null ? '' : gValue;
        gTitle = gTitle==null ? '' : gTitle;
        var arr1= oNode.getAttribute('autofield');
        var datatype = oNode.getAttribute('datatype');
        
		if(arr1){
			arr1= arr1.split('.');
			var field = arr1[1]==null?'':arr1[1];
			//$G('datasource').value = arr1[0];
			//$("#datasource").val(arr1[0]);
			//$("#field").val(field);
        	//$G('field').value = field;
        	var id = '';
			editor.ready(function() {
				id = editor.queryCommandValue('serverparam').id; //返回参数值键值对的对象
			 });
        	$.post(
   				'autoFormController.do?getFormField',		
   				{dbName:arr1[0],autoFormId:id},
   				function(data){
   					data = $.parseJSON(data);
   					$('#field').html('');
   					$('#field').html(data.msg);
   					$('#field').val(field);
   					$("#datasource").val(arr1[0]);
   			});
        }
        
		$G('orgvalue').value = gValue;
        $G('orgname').value = gTitle;
        $G('datatype').value = datatype;
        if (gHidden == '1')
        {
            $G('orghide').checked = true;
        }
        $G('orgfontsize').value = gFontSize;
        $G('orgwidth').value = gWidth;
        $G('orgheight').value = gHeight;
        $G('orgalign').value = gAlign;
        $G('orgtype').value = gType;
        loadDataType(datatype);
    }else
    loadDataType( $G('datatype').value);
}
dialog.oncancel = function () {
    if( UE.plugins[thePlugins].editdom ) {
        delete UE.plugins[thePlugins].editdom;
    }
};
dialog.onok = function (){
    if($G('orgname').value==''){
        var selectedField=$('#field').find("option:selected");
        var flag=false;
        if(selectedField.length>0){
            selectedField=selectedField.get(0);
            if(selectedField.value!=''){
                $G('orgname').value=selectedField.text;
                flag=true;
            }
        }
        if(!flag){
            alert('请输入控件名称');
            $G('orgname').focus();
            return false;
        }
    }
    var gValue=$G('orgvalue').value.replace(/\"/g,"&quot;"),gTitle=$G('orgname').value.replace(/\"/g,"&quot;"),gFontSize=$G('orgfontsize').value,gAlign=$G('orgalign').value,gWidth=$G('orgwidth').value,gHeight=$G('orgheight').value,gType=$G('orgtype').value;
    var datasource=$('#datasource').val();
    var field=$('#field').val();
    var datatype = $G('datatype').value;
    //如果只选择了数据源，没有选择字段，则提示选择字段，使得不会出现在提交后出现只有数据源而没有字段的情况
    if(datasource !='' && field ==''){
    	alert('请选择字段');
    	return false;
    }
    var name= getName(datasource,field,gTitle);
    if( !oNode ) {
        try {
            oNode = createElement('input','leipiNewField');
            oNode.setAttribute('type','text');
            oNode.setAttribute('title',gTitle);
            oNode.setAttribute('value',gValue);
            oNode.setAttribute('datatype',datatype);
            oNode.setAttribute('name',name);
            oNode.setAttribute('leipiPlugins',thePlugins);
            if ( $G('orghide').checked ) {
                oNode.setAttribute('orghide',1);
            } else {
                oNode.setAttribute('orghide',0);
            }
            if( gFontSize != '' ) {
                oNode.style.fontSize = gFontSize + 'px';
                //style += 'font-size:' + gFontSize + 'px;';
                oNode.setAttribute('orgfontsize',gFontSize );
            }
            if( gAlign != '' ) {
                //style += 'text-align:' + gAlign + ';';
                oNode.style.textAlign = gAlign;
                oNode.setAttribute('orgalign',gAlign );
            }
            if( gWidth != '' ) {
                oNode.style.width = gWidth+ 'px';
                //style += 'width:' + gWidth + 'px;';
                oNode.setAttribute('orgwidth',gWidth );
            }
            if( gHeight != '' ) {
                oNode.style.height = gHeight+ 'px';
                //style += 'height:' + gHeight + 'px;';
                oNode.setAttribute('orgheight',gHeight );
            }
            if( gType != '' ) {
                oNode.setAttribute('orgtype',gType );
            }
            
            if( datasource != '' ){
            	oNode.setAttribute('autofield',datasource+(field!=''?('.'+field):'') );
            }
            //oNode.setAttribute('style',style );
            //oNode.style.cssText=style;//ie7
            editor.execCommand('insertHtml',oNode.outerHTML);
        } catch (e) {
            try {
                editor.execCommand('error');
            } catch ( e ) {
                alert('控件异常，请到 [雷劈网] 反馈或寻求帮助！');
            }
            return false;
        }
    } else {
        
        oNode.setAttribute('title', gTitle);
        oNode.setAttribute('value', $G('orgvalue').value);
        oNode.setAttribute('datatype',datatype);
        oNode.setAttribute('name',name);
        if( $G('orghide').checked ) {
            oNode.setAttribute('orghide', 1);
        } else {
            oNode.setAttribute('orghide', 0);
        }
        if( gFontSize != '' ) {
            oNode.style.fontSize = gFontSize+ 'px';
            oNode.setAttribute('orgfontsize',gFontSize );
        }else{
            oNode.style.fontSize = '';
            oNode.setAttribute('orgfontsize', '');
        }
        if( gAlign != '' ) {
            oNode.style.textAlign = gAlign;
            oNode.setAttribute('orgalign',gAlign );
        }else{
            oNode.setAttribute('orgalign', '');
        }
        if( gWidth != '' ) {
            oNode.style.width = gWidth+ 'px';
            oNode.setAttribute('orgwidth',gWidth );
        }else{
            oNode.style.width = '';
            oNode.setAttribute('orgwidth', '');
        }
        if( gHeight != '' ) {
            oNode.style.height = gHeight+ 'px';
            oNode.setAttribute('orgheight',gHeight );
        }else{
            oNode.style.height = '';
            oNode.setAttribute('orgheight', '');
        }
        if( gType != '' ) {
            oNode.setAttribute('orgtype',gType );
        }else{
            oNode.setAttribute('orgtype', '');
        }
        
            if( datasource != '' ){
            	oNode.setAttribute('autofield',datasource+(field!=''?('.'+field):'') );
            }else{
            	oNode.setAttribute('autofield','');
            }
        delete UE.plugins[thePlugins].editdom;
    }
};
//如果选择了数据源及字段，则name为数据源.字段的形式，否则，则为控件名称(title)
function getName(datasource,field,title){
	if(datasource !='' && field !=''){
		return datasource+'.'+field;
	} else {
		return title;
	}
}
	$(function(){
		//1.从参数中获取ID
		var id = '';
		editor.ready(function() {
			id = editor.queryCommandValue('serverparam').id; //返回参数值键值对的对象
		 });
		//2.根据ID进行数据源和字段的填充
		//2.1 填充数据源
		$.post(
			'autoFormController.do?getFormDb',		
			{autoFormId:id},
			function(data){
				data = $.parseJSON(data);
				$('#datasource').html(data.msg);
		});
		//2.2 绑定change事件,并填充字段
		$("#datasource").change(function(){
            var dbId = $(this).val();
            $.post(
                    'autoFormController.do?getFormField',
                    {dbName:dbId,autoFormId:id},
                    function(data){
                        data = $.parseJSON(data);
                        $('#field').html('');
                        $('#field').html(data.msg);
                    });
        });
        $("#field").change(function(){
            if(this.value!='')
                $G('orgname').value= $(this).find("option:selected")[0].text;
        });
	});
function loadDataType(val){
    $("#datatype").empty();
    $("#datatype").append("<option value=''>无</option>");
    $.ajax({
        url:"systemController.do?typeGrid&",
        data:{typegroupid:"4028838850c35b6a0150c37251e00002",field:"id,typename,typecode"},
        type:"post",
        dataType:"json",
        success:function(data){
            var results=data.rows;
            $.each(results,function(i,dict){
               $("#datatype").append("<option value='"+dict.typecode+"'>"+dict.typename+"</option>");
            })
            if(val)
                $($G('datatype')).find("option[value='"+val+"']").attr("selected","selected");
        }
    });
}
</script>
</body>
</html>